package indi.kider.server.bill.api;

import indi.kider.common.model.bill.MeterReadingQuery;
import indi.kider.common.model.bill.Reading;
import indi.kider.server.bill.service.ReadingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;

import java.util.List;

/**
 * Created by shixj on 2018/5/9.
 */
@RestController
@RequestMapping("reading")
public class ReadingController {
    @Autowired
    private ReadingService readingService;


    /**
     * @api {POST} reading/auto/batch
     * @apiGroup balance-bill
     * @apiDescription 后台自动抄表入库多条 （内部服务）
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/reading/auto/batch
     * [{"num":"MX23221","reading":"15.00","type":1,"time":"2018-05-09 16:55:58","method":1,"remark":""}]
     * @apiSuccessExample {json} 返回样例:
     * {"status":"201","msg":"保存成功！","data":""}
     */
    @PostMapping("auto/batch")
    public CommonResponse batchAdd(@RequestBody List<Reading> readings) {
        readingService.batchAdd(readings);
        return CommonResponse.buildAddSuccessResponse("");
    }

    /**
     * @api {POST} reading/auto/single
     * @apiGroup balance-bill
     * @apiDescription 后台自动抄表入库单条（内部服务）
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/reading/auto/single
     * {"num":"MX23221","reading":"15.00","type":1,"time":"2018-05-09 16:55:58","method":1,"remark":""}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"201","msg":"保存成功！","data":""}
     */
    @PostMapping("auto/single")
    public CommonResponse add(@RequestBody Reading reading) {
        return CommonResponse.buildAddSuccessResponse(readingService.add(reading));
    }

    /**
     * @api {POST} reading/list
     * @apiGroup balance-bill
     * @apiDescription 查询读数记录（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/reading/list
     * {"num":"0","size":"8","query":{"id":1,"billAttribution":1,"num":"MX23221","reading":"15.00","type":1,"time":"2018-05-09 16:55:58","method":1,"remark":""}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"id":1,"billAttribution":1,"num":"MX23221","reading":"15.00","type":1,"time":"2018-05-09 16:55:58","method":1,"remark":""}],"total":1,"size":0}}
     */
    @PostMapping("list")
    public CommonResponse query(@RequestBody PageRequest<Reading> request) {
        return CommonResponse.buildGetSuccessResponse(readingService.query(request));
    }

    /**
     * @api {GET} reading/{readingId}
     * @apiGroup balance-bill
     * @apiDescription 根据ID查询读数记录
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/reading/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"id":1,"billAttribution":1,"num":"MX23221","reading":"15.00","type":1,"time":"2018-05-09 16:55:58","method":1,"remark":""}}
     */
    @GetMapping("{readingId}")
    public CommonResponse queryById(@PathVariable Long readingId) {
        return CommonResponse.buildGetSuccessResponse(readingService.queryById(readingId));
    }
    /**
     * @api {POST} reading/mannal
     * @apiGroup balance-bill
     * @apiDescription 智能抄表 meterType:1水2电  rentId:未出租房屋传0或null
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/reading/mannal
     * {"rentId":1,"housesId":70,"meterType":1}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":1.01}
     */
    @PostMapping("mannal")
    public CommonResponse mannalRead(@RequestBody MeterReadingQuery meterReadingQuery) {
        try {
            return CommonResponse.buildGetSuccessResponse(readingService.mannalRead(meterReadingQuery).getUsedValue());
        }catch (Exception e){
            return CommonResponse.buildExceptionResponse(e);
        }
    }
}
